$(function() {
    init_vue();
});

function init_vue() {
    var vue = new Vue({
        el : "#myapp",
        data : {
            startRow : 0,
            endRow : 0,
            total : 0,
            firstPage : 0,
            lastPage : 0,
            pageNum : 1,
            pageSize : 15,
            list : null,
            setting : null,
            navigatepageNums : null
        },
        events : {
            'refreshOrganizationSettingInfo': function (t) {
                for (var i=0; i<this.list.length; i++) {
                    if (this.list[i].id == t.organizationSetting.id) {
                        this.list[i].name = t.organizationSetting.name;
                        this.list[i].wx_appid = t.organizationSetting.wx_appid;
                        this.list[i].wx_secret = t.organizationSetting.wx_secret;
                    }
                }
            },
            'refreshOrganizationSettingList':function () {
                this.pageNum = 1;
                this.getData();
            }
        },
        methods : {
            init : function() {
                close_layuiLayerLoad();
                this.getData();
            },
            showPage : function(page) {
                // alert(page);
                this.pageNum = page;
                this.getData();
            },
            getData : function() {
                showLoad();
                var t = this;
                postMsg(root+"/organizationSetting/tolist", {
                    page_num : t.pageNum,
                    page_size : t.pageSize
                }, function(i) {
                    t.list = null;
                    t.list = i.data.list;
                    t.setting = i.data.setting;
                    t.startRow = i.data.startRow;
                    t.endRow = i.data.endRow;
                    t.total = i.data.total;
                    t.firstPage = i.data.firstPage;
                    t.lastPage = i.data.lastPage;
                    t.navigatepageNums = i.data.navigatepageNums;
                    hideLoad();
                });
            },
            changeState : function(id, state) {
                var t = this;
                showLoad();
                postMsg(root+"/organizationSetting/changeState", {id:id, state:state}, function(i) {
                    if (i == 1) {
                        for (var i=0; i<t.list.length; i++) {
                            if (t.list[i].id == id) {
                                t.list[i].state = state;
                            }
                        }
                        if (state == 2) {
                            $.scojs_message('公众号已停用', $.scojs_message.TYPE_ERROR);
                        } else if (state == 1) {
                            $.scojs_message('公众号已启用', $.scojs_message.TYPE_OK);
                        }
                    }
                    hideLoad();
                });
            },
            openEditOrganizationWeiXinWin: function (id) {
                var t = this;
                var index = layer.load(0, {shade: [0.3, '#000000']}); //0代表加载的风格，支持0-2
                postMsg(root + "/organizationSetting/getOrganizationSettingInfo", {id: id}, function (i) {
                    layer.close(index);
                    var title = '修改公从啧';
                    t.$broadcast('openOrganizationWeiXinWin', {title: title, organizationSetting: i});
                });
            },
            openOrganizationWeiXinWin: function () {
                var t = this;
                var title = '添加公众号';
                this.$broadcast('openOrganizationWeiXinWin', {title:title, organizationSetting:{id:0, name:"", wx_appid:"", wx_secret:"", qrcode_back:"", back_x:"", back_y:"", qrcode_size:"", state:1}});
            },
            gotoInfoPage : function (id) {
                // window.location.href = root + '#user/info/id/' + id;
            }
        }
    });
    vue.init();
}